package com.medical.medicalmodulecommon.response;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;

@Getter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CommonResponse<T> {

    private int code;
    private String message;
    private T data;

    // 构造方法保持不变
    private CommonResponse(int code) {
        this.code = code;
    }

    private CommonResponse(int code, String message) {
        this.code = code;
        this.message = message;
    }

    private CommonResponse(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    private CommonResponse(int code, T data) {
        this.code = code;
        this.data = data;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return this.code == ResponseCode.SUCCESS.getCode();
    }

    @JsonIgnore
    public boolean isError() {
        return this.code == ResponseCode.ERROR.getCode();
    }

    @JsonIgnore
    public boolean isLimits() {
        return this.code == ResponseCode.LIMITS.getCode();
    }

    // ========== 成功相关方法 ==========
    public static <T> CommonResponse<T> createForSuccess() {
        return new CommonResponse<T>(ResponseCode.SUCCESS.getCode());
    }

    public static <T> CommonResponse<T> createForSuccessMessage(String message) {
        return new CommonResponse<>(ResponseCode.SUCCESS.getCode(), message);
    }

    public static <T> CommonResponse<T> createForSuccess(T data) {
        return new CommonResponse<>(ResponseCode.SUCCESS.getCode(), data);
    }

    public static <T> CommonResponse<T> createForSuccess(String message, T data) {
        return new CommonResponse<T>(ResponseCode.SUCCESS.getCode(), message, data);
    }

    // ========== 错误相关方法 ==========
    public static <T> CommonResponse<T> createForError() {
        return new CommonResponse<T>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDescription());
    }

    public static <T> CommonResponse<T> createForError(String message) {
        return new CommonResponse<T>(ResponseCode.ERROR.getCode(), message);
    }

    public static <T> CommonResponse<T> createForError(int code, String message) {
        return new CommonResponse<T>(code, message);
    }

    public static <T> CommonResponse<T> createForError(ResponseCode responseCode) {
        return new CommonResponse<T>(responseCode.getCode(), responseCode.getDescription());
    }

    // ========== 权限不足相关方法 ==========
    public static <T> CommonResponse<T> createForLimits() {
        return new CommonResponse<T>(ResponseCode.LIMITS.getCode(), ResponseCode.LIMITS.getDescription());
    }

    public static <T> CommonResponse<T> createForLimits(String message) {
        return new CommonResponse<T>(ResponseCode.LIMITS.getCode(), message);
    }

    // ========== 通用方法 ==========
    /**
     * 创建响应 - 根据布尔值自动选择成功或失败
     * // 使用条件方法
     * boolean result = userService.updateUser(user);
     * return CommonResponse.createForCondition(result, "更新成功", "更新失败");
     */
    public static <T> CommonResponse<T> createForCondition(boolean success, String successMsg, String errorMsg) {
        return success ? createForSuccessMessage(successMsg) : createForError(errorMsg);
    }

    public static <T> CommonResponse<T> createForCondition(boolean success, T data, String errorMsg) {
        return success ? createForSuccess(data) : createForError(errorMsg);
    }

    /**
     * 快速创建响应
       * // 使用条件方法
     * return CommonResponse.of(ResponseCode.SUCCESS);
     * return CommonResponse.of(ResponseCode.LIMITS, "您没有权限访问此功能");
     * return CommonResponse.of(ResponseCode.SUCCESS, user);
     */
    public static <T> CommonResponse<T> of(ResponseCode responseCode) {
        return new CommonResponse<T>(responseCode.getCode(), responseCode.getDescription());
    }

    public static <T> CommonResponse<T> of(ResponseCode responseCode, String message) {
        return new CommonResponse<T>(responseCode.getCode(), message);
    }

    public static <T> CommonResponse<T> of(ResponseCode responseCode, T data) {
        return new CommonResponse<T>(responseCode.getCode(), responseCode.getDescription(), data);
    }
}